﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuaCheng.Model
{
    public static class CState
    {
        /// <summary>
        /// 从ARM接受到的数据解析相应通道的状态
        /// </summary>
        /// <param name="stateData">总的状态数据包</param>
        /// <param name="chNum">通道号</param>
        /// <param name="chStateLenth">通道状态数据长度</param>
        /// <returns>通道状态类</returns>
        public static ChannelState byte2Channel(byte[] stateData, int chNum, int chStateLenth)
        {
            if (stateData.Length < 612) return null;

            ChannelState tmpChannel = new ChannelState();
            byte[] chStateArray = new byte[chStateLenth];
            Array.Copy(stateData, 2 + (chNum - 1) * chStateLenth, chStateArray, 0, chStateLenth);//拷贝通道数据
            tmpChannel.stepNum = BitConverter.ToUInt16(chStateArray, 0) >> 4;
            tmpChannel.GID = BitConverter.ToUInt16(chStateArray, 2) & 0x0FFF;
            tmpChannel.CID = BitConverter.ToUInt16(chStateArray, 2) >> 12;
            //AC状态1
            tmpChannel.ACstate1_BusUnderVolt = (chStateArray[4] & 0x01) == 0x01 ? true : false;
            tmpChannel.ACstate1_PhaseError = (chStateArray[4] & 0x02) == 0x02 ? true : false;
            tmpChannel.ACstate1_ACUnderVolt = (chStateArray[4] & 0x04) == 0x04 ? true : false;
            tmpChannel.ACstate1_ACLoseVolt = (chStateArray[4] & 0x08) == 0x08 ? true : false;
            tmpChannel.ACstate1_rsv4 = (chStateArray[4] & 0x10) == 0x10 ? true : false;
            tmpChannel.ACstate1_rsv5 = (chStateArray[4] & 0x20) == 0x20 ? true : false;
            tmpChannel.ACstate1_rsv6 = (chStateArray[4] & 0x40) == 0x40 ? true : false;
            tmpChannel.ACstate1_rsv7 = (chStateArray[4] & 0x80) == 0x80 ? true : false;
            tmpChannel.ACstate1_rsv8 = (chStateArray[5] & 0x01) == 0x01 ? true : false;
            tmpChannel.ACstate1_AC_fuse = (chStateArray[5] & 0x02) == 0x02 ? true : false;
            tmpChannel.ACstate1_emergency = (chStateArray[5] & 0x04) == 0x04 ? true : false;
            tmpChannel.ACstate1_AC_overTemper = (chStateArray[5] & 0x08) == 0x08 ? true : false;
            tmpChannel.ACstate1_AC_ACOverVoltHard = (chStateArray[5] & 0x10) == 0x10 ? true : false;
            tmpChannel.ACstate1_AC_DCOverVoltHard = (chStateArray[5] & 0x20) == 0x20 ? true : false;
            tmpChannel.ACstate1_AC_ACOverCurrentHard = (chStateArray[5] & 0x40) == 0x40 ? true : false;
            tmpChannel.ACstate1_AC_paramError = (chStateArray[5] & 0x80) == 0x80 ? true : false;

            //AC状态2
            tmpChannel.ACstate2_AC_ACOverVoltSoft = (chStateArray[6] & 0x01) == 0x01 ? true : false;
            tmpChannel.ACstate2_AC_DCOverVoltSoft = (chStateArray[6] & 0x02) == 0x02 ? true : false;
            tmpChannel.ACstate2_AC_ACOverCurrentSoft = (chStateArray[6] & 0x04) == 0x04 ? true : false;
            tmpChannel.ACstate2_AC_breakFault = (chStateArray[6] & 0x08) == 0x08 ? true : false;
            tmpChannel.ACstate2_PLL = (chStateArray[6] & 0x10) == 0x10 ? true : false;
            tmpChannel.ACstate2_AC_highVolt = (chStateArray[6] & 0x20) == 0x20 ? true : false;
            tmpChannel.ACstate2_gridFault = (chStateArray[6] & 0x40) == 0x40 ? true : false;
            tmpChannel.ACstate2_AC_preCharge = (chStateArray[6] & 0x80) == 0x80 ? true : false;
            tmpChannel.ACstate2_AC_breakState = (chStateArray[7] & 0x01) == 0x01 ? true : false;
            tmpChannel.ACstate2_rsv0 = (chStateArray[7] & 0x02) == 0x02 ? true : false;
            tmpChannel.ACstate2_AC_workState = (chStateArray[7] & 0x04) == 0x04 ? true : false;
            tmpChannel.ACstate2_AC_fault = (chStateArray[7] & 0x08) == 0x08 ? true : false;
            tmpChannel.ACstate2_AC_preChargeFault = (chStateArray[7] & 0x10) == 0x10 ? true : false;
            tmpChannel.ACstate2_AC_UPS = (chStateArray[7] & 0x20) == 0x20 ? true : false;
            tmpChannel.ACstate2_DC_dischargeState = (chStateArray[7] & 0x40) == 0x40 ? true : false;
            tmpChannel.ACstate2_DC_dischargeError = (chStateArray[7] & 0x80) == 0x80 ? true : false;

            tmpChannel.gridFreq = BitConverter.ToUInt16(chStateArray, 8);

            tmpChannel.DCstate1_DC_workState = (chStateArray[10] & 0x01) == 0x01 ? true : false;
            tmpChannel.DCstate1_rsv0 = (chStateArray[10] & 0x02) == 0x02 ? true : false;
            tmpChannel.DCstate1_DC_fault = (chStateArray[10] & 0x04) == 0x04 ? true : false;
            tmpChannel.DCstate1_rsv1 = (chStateArray[10] & 0x08) == 0x08 ? true : false;
            tmpChannel.DCstate1_rsv2 = (chStateArray[10] & 0x10) == 0x10 ? true : false;
            tmpChannel.DCstate1_DC_preChargeFault = (chStateArray[10] & 0x20) == 0x20 ? true : false;
            tmpChannel.DCstate1_rsv3 = (chStateArray[10] & 0x40) == 0x40 ? true : false;
            tmpChannel.DCstate1_rsv4 = (chStateArray[10] & 0x80) == 0x80 ? true : false;
            tmpChannel.DCstate1_Contactor = (chStateArray[11] & 0x01) == 0x01 ? true : false;
            tmpChannel.DCstate1_rsv5 = (chStateArray[11] & 0x02) == 0x02 ? true : false;
            tmpChannel.DCstate1_Fan = (chStateArray[11] & 0x04) == 0x04 ? true : false;
            tmpChannel.DCstate1_rsv6 = (chStateArray[11] & 0x08) == 0x08 ? true : false;
            tmpChannel.DCstate1_rsv7 = (chStateArray[11] & 0x10) == 0x10 ? true : false;
            tmpChannel.DCstate1_rsv8 = (chStateArray[11] & 0x20) == 0x20 ? true : false;
            tmpChannel.DCstate1_rsv9 = (chStateArray[11] & 0x40) == 0x40 ? true : false;
            tmpChannel.DCstate1_rsv10 = (chStateArray[11] & 0x80) == 0x80 ? true : false;

            //DC状态
            tmpChannel.DCstate_rsv0 = (chStateArray[12] & 0x01) == 0x01 ? true : false;
            tmpChannel.DCstate_rsv1 = (chStateArray[12] & 0x02) == 0x02 ? true : false;
            tmpChannel.DCstate_rsv2 = (chStateArray[12] & 0x04) == 0x04 ? true : false;
            tmpChannel.DCstate_rsv3 = (chStateArray[12] & 0x08) == 0x08 ? true : false;
            tmpChannel.DCstate_DC_breakFault = (chStateArray[12] & 0x10) == 0x10 ? true : false;
            tmpChannel.DCstate_rsv4 = (chStateArray[12] & 0x20) == 0x20 ? true : false;
            tmpChannel.DCstate_DC_inputOverVoltHard = (chStateArray[12] & 0x40) == 0x40 ? true : false;
            tmpChannel.DCstate_DC_outputOverVoltHard = (chStateArray[12] & 0x80) == 0x80 ? true : false;
            tmpChannel.DCstate_DC_loadOverCurrentHard = (chStateArray[13] & 0x01) == 0x01 ? true : false;
            tmpChannel.DCstate_DC_inductorOverCurrentHard = (chStateArray[13] & 0x02) == 0x02 ? true : false;
            tmpChannel.DCstate_DC_inputOverVoltSoft = (chStateArray[13] & 0x04) == 0x04 ? true : false;
            tmpChannel.DCstate_DC_outputOverVoltSoft = (chStateArray[13] & 0x08) == 0x08 ? true : false;
            tmpChannel.DCstate_DC_loadOverCurrentSoft = (chStateArray[13] & 0x10) == 0x10 ? true : false;
            tmpChannel.DCstate_DC_inductorOverCurrentSoft = (chStateArray[13] & 0x20) == 0x20 ? true : false;
            tmpChannel.DCstate_DC_overTemper = (chStateArray[13] & 0x40) == 0x40 ? true : false;
            tmpChannel.DCstate_DC_highVolt = (chStateArray[13] & 0x80) == 0x80 ? true : false;

            //DC停机原因
            tmpChannel.DCoff_voltageUpper = (chStateArray[14] & 0x01) == 0x01 ? true : false;
            tmpChannel.DCoff_DCoffvoltageLower = (chStateArray[14] & 0x02) == 0x02 ? true : false;
            tmpChannel.DCoff_currentUpper = (chStateArray[14] & 0x04) == 0x04 ? true : false;
            tmpChannel.DCoff_currentLower = (chStateArray[14] & 0x08) == 0x08 ? true : false;
            tmpChannel.DCoff_timeout = (chStateArray[14] & 0x10) == 0x10 ? true : false;
            tmpChannel.DCoff_temperUpper = (chStateArray[14] & 0x20) == 0x20 ? true : false;
            tmpChannel.DCoff_temperLower = (chStateArray[14] & 0x40) == 0x40 ? true : false;
            tmpChannel.DCoff_systemFault = (chStateArray[14] & 0x80) == 0x80 ? true : false;
            tmpChannel.DCoff_offCommand = (chStateArray[15] & 0x01) == 0x01 ? true : false;
            tmpChannel.DCoff_emergency = (chStateArray[15] & 0x02) == 0x02 ? true : false;
            tmpChannel.DCoff_ARM_auxTemper = (chStateArray[15] & 0x04) == 0x04 ? true : false;
            tmpChannel.DCoff_ARM_auxVolt = (chStateArray[15] & 0x08) == 0x08 ? true : false;
            tmpChannel.DCoff_ARM_emergency = (chStateArray[15] & 0x10) == 0x10 ? true : false;
            tmpChannel.DCoff_rsv0 = (chStateArray[15] & 0x20) == 0x20 ? true : false;
            tmpChannel.DCoff_rsv1 = (chStateArray[15] & 0x40) == 0x40 ? true : false;
            tmpChannel.DCoff_rsv2 = (chStateArray[15] & 0x80) == 0x80 ? true : false;

            tmpChannel.DC_outputVolt = BitConverter.ToSingle(chStateArray, 16);
            tmpChannel.DC_loadCurrent = BitConverter.ToSingle(chStateArray, 20);
            tmpChannel.DC_loadTemper = BitConverter.ToUInt16(chStateArray, 24);
            tmpChannel.DC_topTemper = BitConverter.ToUInt16(chStateArray, 26);
            tmpChannel.DC_busVolt = BitConverter.ToUInt16(chStateArray, 28);
            tmpChannel.chargeCap = BitConverter.ToSingle(chStateArray, 30);
            tmpChannel.dischargeCap = BitConverter.ToSingle(chStateArray, 34);
            tmpChannel.totalCap = BitConverter.ToSingle(chStateArray, 38);
            tmpChannel.stepTime = BitConverter.ToSingle(chStateArray, 42);
            tmpChannel.chargeEnergy = BitConverter.ToSingle(chStateArray, 46);
            tmpChannel.dischargeEnergy = BitConverter.ToSingle(chStateArray, 50);
            tmpChannel.totalEnergy = BitConverter.ToSingle(chStateArray, 54);
            tmpChannel.power = BitConverter.ToSingle(chStateArray, 58);
            tmpChannel.diffVolt = BitConverter.ToSingle(chStateArray, 62);
            tmpChannel.diffCurrent = BitConverter.ToSingle(chStateArray, 66);
            tmpChannel.auxTemper = BitConverter.ToUInt16(chStateArray, 70);
            tmpChannel.auxVolt = BitConverter.ToSingle(chStateArray, 72);

            return tmpChannel;
        }
    }
}
